package com.yishun.springboot.gwsutil;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.requestGwsLongGangQu.GwsLgqUtil;
import com.yinquan.util.DataUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;


@Slf4j
@Component
public class Elevator {

    private final String api_version = "1.0";


    /**
     * 深圳市工务署，龙岗区工务署公用方法  上传基础信息
     */
    public JSONObject addBasicInfo(JSONObject jsonParam, String httpserver, String api_key, String engCode, String projectCode) {
        String url = httpserver + "/elevator/addBasicInfo";

        String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

        String body = JSON.toJSONString(jsonParam);

        String signature = GwsLgqUtil.getSignature(api_key, api_version, engCode, projectCode, timestamp, body);

        //封装请求参数
        String res = GwsLgqUtil.GwsParamUtil(jsonParam, timestamp, signature, api_key, projectCode, engCode, api_version);

        String info = HttpUtil.post(url, res);

        return JSONObject.parseObject(info);

    }


    /**
     * 深圳市工务署，龙岗区工务署公用方法  上传参数信息
     */

    public JSONObject addParams(JSONObject jsonParam, String httpserver, String api_key, String engCode, String projectCode) {
        String url = httpserver + "/elevator/addParams";

        String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

        String body = JSON.toJSONString(jsonParam);

        String signature = GwsLgqUtil.getSignature(api_key, api_version, engCode, projectCode, timestamp, body);

        //封装请求参数
        String res = GwsLgqUtil.GwsParamUtil(jsonParam, timestamp, signature, api_key, projectCode, engCode, api_version);

        String info = HttpUtil.post(url, res);

        return JSONObject.parseObject(info);
    }


    /**
     * 深圳市工务署，龙岗区工务署公用方法  上传实时数据
     */
    public JSONObject pushElevatorData(JSONObject jsonParam, String httpserver, String api_key, String engCode, String projectCode) {
        String url = httpserver + "/elevator/addRecord";

        String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

        List<Object> arrayList = new ArrayList<Object>();

        arrayList.add(jsonParam);

        String body = JSONArray.toJSONString(arrayList);

        String signature = GwsLgqUtil.getSignature(api_key, api_version, engCode, projectCode, timestamp, body);

        //封装请求参数
        String res = GwsLgqUtil.GwsParamUtil(arrayList, timestamp, signature, api_key, projectCode, engCode, api_version);

        String info = HttpUtil.post(url, res);

        return JSONObject.parseObject(info);
    }


    /**
     * 龙岗区工务署上传操作数据
     */
    public JSONObject pushElevatorOperator(JSONObject jsonParam, String httpserver, String api_key, String engCode, String projectCode) {
        String url = httpserver + "elevator/operator";

        String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

        List<Object> arrayList = new ArrayList<Object>();

        arrayList.add(jsonParam);

        String body = JSONArray.toJSONString(arrayList);

        String signature = GwsLgqUtil.getSignature(api_key, api_version, engCode, projectCode, timestamp, body);

        //封装请求参数
        String res = GwsLgqUtil.GwsParamUtil(arrayList, timestamp, signature, api_key, projectCode, engCode, api_version);

        String info = HttpUtil.post(url, res);

        return JSONObject.parseObject(info);
    }


    public void pushElevatorEnergising(JSONObject jsonParam, String httpserver, String api_key, String engCode, String projectCode) {
        String url = httpserver + "elevator /electrify";

        String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

        List<Object> arrayList = new ArrayList<Object>();

        arrayList.add(jsonParam);

        String body = JSONArray.toJSONString(arrayList);

        String signature = GwsLgqUtil.getSignature(api_key, api_version, engCode, projectCode, timestamp, body);
        //封装请求参数
        String res = GwsLgqUtil.GwsParamUtil(arrayList, timestamp, signature, api_key, projectCode, engCode, api_version);

        String info = HttpUtil.post(url, res);

        log.info(info);
    }

    public static void main(String[] args) {

//        String url = "http://218.17.147.31:66/openapi/elevator/operator";
//
//        String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
//
//        JSONObject jsonParam = new JSONObject();
//        jsonParam.put("device_no", "d3f3a87770c0466c8b03bb6cb24aa014");
//        jsonParam.put("start_time", "2023-11-17 15:00:00");
//        jsonParam.put("end_time", "2023-11-17 15:03:00");
//        //jsonParam.put("direction", 1);
//        //jsonParam.put("max_load_value", 50.0);
//        jsonParam.put("is_over_load", 0);
//        //jsonParam.put("operator_name", "管理员");
//        jsonParam.put("isIllegal_operation", 1);
//
//        List<Object> arrayList = new ArrayList<Object>();
//
//        arrayList.add(jsonParam);
//
//        String body = JSONArray.toJSONString(arrayList);
//
//        String signature = GwsLgqUtil.getSignature("c34243b5b594406d96ad85d75112f5f2", "1.0", "XM20180258", "XM20180258", timestamp, body);
//
//        String res = GwsLgqUtil.GwsParamUtil(arrayList, timestamp, signature, "c34243b5b594406d96ad85d75112f5f2", "XM20180258", "XM20180258", "1.0");
//
//        String info = HttpUtil.post(url, res);
//
//        log.info(info);


        String startTime = DataUtil.getScopeRandomTime(6, 0, 0, 6, 30, 0);
        String endTime = DataUtil.getScopeRandomTime(20, 0, 0, 20, 30, 0);

        System.out.println("随机时间: " + startTime + "-----" + endTime);


        // 定义日期时间范围（开始时间和结束时间）
        LocalDateTime startDateTime = LocalDateTime.of(2022, 1, 1, 0, 0, 0);
        LocalDateTime endDateTime = LocalDateTime.of(2022, 12, 31, 23, 59, 59);

        // 生成随机时间戳
        long startTimestamp = startDateTime.toEpochSecond(java.time.ZoneOffset.UTC);
        long endTimestamp = endDateTime.toEpochSecond(java.time.ZoneOffset.UTC);
        long randomTimestamp = ThreadLocalRandom.current().nextLong(startTimestamp, endTimestamp);

        // 转换为 LocalDateTime
        LocalDateTime randomDateTime = LocalDateTime.ofEpochSecond(randomTimestamp, 0, java.time.ZoneOffset.UTC);

        String time = randomDateTime.format(DateTimeFormatter.ofPattern("yy-MM-dd HH:mm:ss"));

        System.out.println("随机日期时间: " + time);
    }

}
